{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d0dd36e6",
   "metadata": {},
   "source": [
    "---\n",
    "* 2023-10-18\n",
    "* 课程：Python语言\n",
    "* week05\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b69299a7",
   "metadata": {},
   "source": [
    "# 1.用户输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f8ed2820",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的账号Kelly\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Kelly'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input(\"请输入你的账号\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f474e521",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的账号：Kelly\n",
      "请输入你的密码：123456\n"
     ]
    }
   ],
   "source": [
    "username = input(\"请输入你的账号：\")\n",
    "password = input(\"请输入你的密码：\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "62273deb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Kelly\n",
      "123456\n"
     ]
    }
   ],
   "source": [
    "print(username)\n",
    "print(password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "312f454f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的账号：Kell\n",
      "请输入你的密码：123456\n",
      "账号或密码错误，登录失败\n"
     ]
    }
   ],
   "source": [
    "# 假设 正确的账号：Kelly\n",
    "# 假设 正确的密码：123456\n",
    "\n",
    "# 登录系统\n",
    "username = input(\"请输入你的账号：\")\n",
    "password = input(\"请输入你的密码：\")\n",
    "if username == \"Kelly\" and password == \"123456\":\n",
    "    print(\"欢迎登录\")\n",
    "else:\n",
    "    print(\"账号或密码错误，登录失败\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80f1cfef",
   "metadata": {},
   "source": [
    "* 笔记：\n",
    "> 1. == 代表判断左右两边是否相等，即“等于”\n",
    "> 2. 所有的符号要用英文的符号，例如冒号:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4ca6d2f",
   "metadata": {},
   "source": [
    "# 2.用户输出（格式化）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "799e03f6",
   "metadata": {},
   "source": [
    "## 2.1 注释\n",
    "> 1. \\# 表示单行注释\n",
    "> 2. “““ 注释内容 ”””多行注释，用于记录之前写过的程序且不会被执行\n",
    "> 3. 注释的内容（code）是不会被执行的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "432ceabc",
   "metadata": {},
   "source": [
    "## 2.2 格式化输出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4333afba",
   "metadata": {},
   "source": [
    "### 2.2.1 字符串的拼接实现格式化输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "93cbc935",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的姓名：Kelly\n",
      "请输入你的年龄：19\n",
      "请输入你的职业：Student\n",
      "请输入你的薪水：50000\n"
     ]
    }
   ],
   "source": [
    "# 指定格式的简历\n",
    "\n",
    "#用户输入信息\n",
    "name = input(\"请输入你的姓名：\")\n",
    "age = input(\"请输入你的年龄：\")\n",
    "job = input(\"请输入你的职业：\")\n",
    "salary = input(\"请输入你的薪水：\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "46227574",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------- INFO Kelly --------\n",
      "姓名:Kelly\n",
      "年龄:19\n",
      "工作:Student\n",
      "薪水:50000\n"
     ]
    }
   ],
   "source": [
    "# 格式化输出内容\n",
    "\n",
    "INFO = \"\"\"-------- INFO \"\"\" + name + \"\"\" --------\n",
    "姓名:\"\"\" + name + \"\"\"\n",
    "年龄:\"\"\" + age + \"\"\"\n",
    "工作:\"\"\" + job + \"\"\"\n",
    "薪水:\"\"\" + salary \n",
    "\n",
    "print(INFO)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6867ec46",
   "metadata": {},
   "source": [
    "* 笔记：上述代码使用的方法是“字符串的拼接”，用+号实现"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e1a5f7f",
   "metadata": {},
   "source": [
    "### 2.2.2 占位符实现格式化输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "125486af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------- INFO Kelly --------\n",
      "姓名:Kelly\n",
      "年龄:19\n",
      "工作:Student\n",
      "薪水:50000 \n"
     ]
    }
   ],
   "source": [
    "# 格式化输出内容\n",
    "\n",
    "INFO = \"\"\"-------- INFO %s --------\n",
    "姓名:%s\n",
    "年龄:%s\n",
    "工作:%s\n",
    "薪水:%s \"\"\"%(name,name,age,job,salary)\n",
    "\n",
    "print(INFO)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19955e4d",
   "metadata": {},
   "source": [
    "* 笔记：占位符比较适用于格式化字段较少的文本处理\n",
    "> 1. %s 代表占位符是字符串\n",
    "> 2. %d 代表占位符是数值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d70d7655",
   "metadata": {},
   "source": [
    "### 2.2.3 格式化方法：format()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5d85e3fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------- INFO Kelly --------\n",
      "姓名:Kelly\n",
      "年龄:19\n",
      "工作:Student\n",
      "薪水:50000 \n"
     ]
    }
   ],
   "source": [
    "# 格式化输出内容\n",
    "\n",
    "# N:姓名 ，A:年龄，J:工作，S:薪水\n",
    "INFO = \"\"\"-------- INFO {N} --------\n",
    "姓名:{N}\n",
    "年龄:{A}\n",
    "工作:{J}\n",
    "薪水:{S} \"\"\".format(N = name, J = job, S = salary, A = age)\n",
    "\n",
    "print(INFO)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abbef3a9",
   "metadata": {},
   "source": [
    "* 笔记：format方法可以解决占位符的两个痛点：\n",
    "> 1. 不再考量位置顺序\n",
    "> 2. 解决重复字段出现的问题，只需要赋值一次，如上述：N = name，只需要写一次即可"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42c236a7",
   "metadata": {},
   "source": [
    "# 3. 课本第一章的三个项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "157b7054",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 3.1 项目一：判断时钟奇数分钟"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e11a777c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Not an odd minute\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]\n",
    "\n",
    "right_this_minute = datetime.today().minute\n",
    "\n",
    "if right_this_minute in odds:\n",
    "    print(\"This minute seems a little odd.\")\n",
    "else:\n",
    "    print(\"Not an odd minute\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b26cbc84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "24"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "datetime.today().second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07a5aa4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 3.2 项目二：执行5次，每次等待随机秒钟，输出当前秒钟为奇数或偶数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ff6c6840",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This minute seems a little odd.\n",
      "Not an odd minute\n",
      "Not an odd minute\n",
      "Not an odd minute\n",
      "Not an odd minute\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "import random\n",
    "import time\n",
    "\n",
    "odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]\n",
    "\n",
    "for i in range(5):\n",
    "    right_this_minute = datetime.today().second\n",
    "    if right_this_minute in odds:\n",
    "        print(\"This minute seems a little odd.\")\n",
    "    else:\n",
    "        print(\"Not an odd minute\")\n",
    "    wait_time = random.randint(1,5)\n",
    "    time.sleep(wait_time)#时间静止wait_time的时间\n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "eda7014a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2e88e4e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7901872624338859\n",
      "0.4141533058719493\n",
      "0.6578939059303767\n",
      "0.10367708265348896\n",
      "0.12371448059915813\n",
      "0.2693025025569652\n",
      "0.7545303752038962\n",
      "0.9203569334710502\n",
      "0.8322204425061903\n",
      "0.600274167115783\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print(random.random())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c86e405",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
